<template>
  <div class="login">
    <!-- 登录表单 s-->
    <el-card :class="[isMobile ? 'login-card m' : 'login-card']">
      <div slot="header">
        <i class="el-icon-monitor mr-5"></i>
        <h3 class="login-form-title font-16 font-w-500">欢迎登陆后台管理系统</h3>
      </div>
      <el-form :model="form" :rules="rules" ref="loginForm">
        <el-form-item prop="username">
          <el-input v-model="form.username" @keyup.enter.native="login" prefix-icon="el-icon-user" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" @keyup.enter.native="login" prefix-icon="el-icon-lock" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
      </el-form>
      <el-button :loading="loading" @click="login" class="mt-10 full-width" type="primary">登录</el-button>
      <p class="login-tip mt-20 font-12 text-center">如忘记账号或密码，请联系系统管理员</p>
    </el-card>
    <!-- 登录表单 e-->

    <!-- footer s-->
    <div class="footer">
      <div class="refer font-14 flex flex-align-center">
        <span>推荐使用</span>
        <img alt="google" src="https://download.uce.cn/update/ucp/prod/resource/images/icon/google.png">
        <a href="javascript:void(0);">谷歌浏览器</a>
      </div>
      <p class="copyright mt-10 font-12 text-center">© 2021 xxx版权所有</p>
    </div>
    <!-- footer s-->
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'Login',
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  computed: {
    ...mapGetters([ 'isMobile' ])
  },
  methods: {
    login () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          setTimeout(() => {
            const token = 'authorization'
            this.$store.commit('setToken', token)
            // token存在cookie, 3小时过期
            this.$cookie.set('adminToken', token, { expires: 1 / 8 })
            this.loading = false
            this.$router.push('/home')
          }, 1000)
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.login {
  width: 100vw;
  height: 100vh;
  background-image: url("../assets/img/login-bg.svg");
  background-size: cover;
  position: relative;
}

.login-card {
  width: 320px;
  position: absolute;
  right: 160px;
  top: 50%;
  transform: translateY(-60%);
}

.login-card.m {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.login-form-title {
  display: inline-block;
  color: #303133;
}

.login-tip {
  color: #c3c3c3;
}

.footer {
  position: absolute;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  .refer {
    img {
      margin: 0 4px;
    }
    a {
      color: #409eff;
      text-decoration: none;
    }
  }
  .copyright {
    opacity: .5;
  }
}
</style>
